<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;
use app\common\model\BonusLog;
use app\common\model\Idcard;
use app\common\model\Member;
use think\Controller;
use think\Db;
use app\common\model\UserAccount;
use app\common\model\UserAccountLog;


class ThirdApplication extends ApiBase
{
   public function _initialize()
   {
       parent::_initialize(); // TODO: Change the autogenerated stub
       $this->api_key = getConfig("hotel_train_flight_api_key");
       $this->api_secret = getConfig("hotel_train_flight_api_secret");
       $this->api_url = getConfig("hotel_train_flight_api_url");
       $this->oil_mobile_app_key = getConfig("oil_mobile_app_key");
       $this->oil_mobile_open_id = getConfig("oil_mobile_open_id");
       $this->oil_mobile_telcheck_url = getConfig("oil_mobile_telcheck_url");
       $this->oil_mobile_telquery_url = getConfig("oil_mobile_telquery_url");
       $this->oil_mobile_submit_url = getConfig("oil_mobile_submit_url");
       $this->oil_mobile_sta_url = getConfig("oil_mobile_sta_url");
       $this->didi_api_url = getConfig("didi_api_url");
       $this->didi_secret_key = getConfig("didi_secret_key");
       $this->didi_ensure_amount = getConfig("didi_ensure_amount");
       $this->application_payment_limit_time = getConfig("application_payment_limit_time");
       $this->end_time = getConfig("application_payment_limit_time") * 60;
   }


    //酒店搜索
    public function hotel_search(){
        $city_name = input("city_name");
        $esd_id = input("esd_id");
        $star = input("star");
        $page_size = input("list_rows");
        $page_index = input("page");
        $data = array(
            "PageSize"=>$page_size,
            "PageIndex"=>$page_index,
            "CityName"=>$city_name,
        );
        if(!empty($esd_id)){
            $data['Esdid'] = $esd_id;
        }
        if(!empty($star)){
            $data['Star'] = $star;
        }
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Hotel/SearchHotelList",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(0,lang('请求失败')),[]);
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));
        }
    }

    //酒店详情
    public function hotel_detail(){
        $h_id = input("h_id");
        $data = array(
            "HId"=>$h_id,
        );
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Hotel/HotelDetail",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));
        }
    }

    //入住日期内酒店房型
    public function hotel_rooms(){
        $h_id = input("h_id");
        $in_date = input("in_date");
        $out_date = input("out_date");
        $data = array(
            "HId"=>[$h_id],
            "InDate"=>$in_date,
            "OutDate"=>$out_date
        );
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Hotel/HotelRooms",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));
        }
    }

    //酒店提交订单
    public function hotel_create_order(){
        $h_id = input("h_id");
        $room_id = input("room_id");
        $product_id = input("product_id");
        $is_guarantee = input("is_guarantee");
        $in_date = input("in_date");
        $out_date = input("out_date");
        $latest_time = input("latest_time");
        $order_amount = input("order_amount");
        $rooms_count = input("rooms_count");
        $passengers = input("passengers");
        $contact_name = input("contact_name");
        $contact_mobile = input("contact_mobile");
        $hotel_name = input("hotel_name");
        $room_name = input("room_name");
        $product_name = input("product_name");
        $userid = decoded_user_token(input("user_token"),'member_id');

        $data = array(
            "user_id"=>$userid,
            "sn"=>  "Hotel".$userid.time(),
            "h_id"=>$h_id,
            "room_id"=>$room_id,
            "product_id"=>$product_id,
            "is_guarantee"=>$is_guarantee,
            "in_date"=>$in_date,
            "out_date"=>$out_date,
            "latest_time"=>$latest_time,
            "order_amount"=>$order_amount,
            "rooms_count"=>$rooms_count,
            "passengers"=>$passengers,
            "contact_name"=>$contact_name,
            "contact_mobile"=>$contact_mobile,
            "create_time"=>time(),
            "update_time"=>time(),
            "cancel_time"=>time()+60*$this->application_payment_limit_time,
            "hotel_name" => $hotel_name,
            "room_name" => $room_name,
            "product_name"=>$product_name,
            "status"=>1
        );
        foreach ($data as $k=>$v){
            if(empty($v)){
                echo json_encode(ajaxreturn(1,lang('参数错误')));
            }
        }
        $res = Db::name("hotel_application_order")->insert($data);
        if(!$res){
            echo json_encode(ajaxreturn(1,lang('提交失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('提交成功')));
        }
    }

    //酒店订单支付
    public function hotel_order_pay(){
        $id = input('id');
        $order = Db::name("hotel_application_order")->where(["id"=>$id,"status"=>1])->find();
        if(empty($order)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));
        }
        $userid = decoded_user_token(input("user_token"),'member_id');
        $member = Db::name('member')->where(['id'=>$userid])->find();
        //判断用户余额
        if($member['app_balance']<$order['order_amount']){
            echo json_encode(ajaxreturn(1,lang('应用余额不足')));exit;
        }
        Db::startTrans();
        try{
            Db::name("member")->where(["id"=>$userid])->setDec("app_balance");
            $log = array(
                "user_id"=>$userid,
                "type"=>6,
                'is_add'=>2,
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
                'serial_number'=>$order['sn'],
                'number'=> $order['order_amount']
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id"=>$userid,
                "type"=>4,
                'number'=> -$order['order_amount']
            ]);
            Db::name('hotel_application_order')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));
        }
    }

    //酒店订单列表
    public function hotel_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("hotel_application_order")->where(["user_id"=>$userid])->select()?Db::name("hotel_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$list));
    }

    //酒店订单详情
    public function hotel_order_detail(){
        $id = input("id");
        if(empty($id)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));
        }
        $detail = Db::name("hotel_application_order")->where(["id"=>$id])->find();
        if(empty($detail)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$detail));
    }

    //火车票搜索
    public function train_search(){
        $from_station = input("from_station");
        $to_station = input("to_station");
        $from_date = input("from_date");
        $type = input("type");
        $period = input("period");
        $sort_time = input("sort_time");
        $sort_consuming = input("sort_consuming");
        $data = array(
            "FromStation"=>$from_station,
            "ToStation"=>$to_station,
            "FromDate"=>$from_date
        );
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Train/Query",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(0,lang($result['Message']),[]));exit;
        }else{
            $data = $result['Data'];

            if((!empty($type)) || ($type==0)){

                foreach ($data as $k=>$v){

                    if($v["TrainType"]!=$type){

                        unset($data[$k]);
                    }

                }
            }
            if(!empty($period)){
                //00:00-12:00
                if($period==1){
                    foreach ($data as $k=>$v){
                        if(time_to_seconds($v["GoTime"])>time_to_seconds("12:00")){
                            unset($data[$k]);
                        }
                    }
                }
                //12:00-18:00
                if($period==2){
                    foreach ($data as $k=>$v){

                        if((time_to_seconds($v["GoTime"])>time_to_seconds("18:00")) || (time_to_seconds($v["GoTime"])<time_to_seconds("12:00"))){

                            unset($data[$k]);
                        }
                    }
                }
                //18:00-24:00
                if($period==3){
                    foreach ($data as $k=>$v){
                        if(time_to_seconds($v["GoTime"])<time_to_seconds("18:00")){
                            unset($data[$k]);
                        }
                    }
                }
            }
            if(empty($data)){
                echo json_encode(ajaxreturn(0,lang('请求成功'),[]));exit;
            }
            $result_data = [];
            foreach ($data as $v){
                $result_data[]=$v;
            }
            if(!empty($sort_time)){
                for ($i = 0; $i < count($result_data) ; $i++) {
                    for ($j = $i+1; $j < count($result_data); $j++) {
                        if (time_to_seconds($result_data[$i]["GoTime"]) > time_to_seconds($result_data[$j]["GoTime"])) {
                            $tem = $result_data[$i]; // 这里临时变量，存贮$i的值
                            $result_data[$i] = $result_data[$j]; // 第一次更换位置
                            $result_data[$j] = $tem; // 完成位置互换
                        }
                    }
                }
            }

            if(!empty($sort_consuming)){
                for ($i = 0; $i < count($result_data) ; $i++) {
                    for ($j = $i+1; $j < count($result_data); $j++) {
                        if ($result_data[$i]["RunTime"]> $result_data[$j]["RunTime"]) {

                            $tem = $result_data[$i]; // 这里临时变量，存贮$i的值
                            $result_data[$i] = $result_data[$j]; // 第一次更换位置
                            $result_data[$j] = $tem; // 完成位置互换
                        }
                    }
                }
            }

             echo json_encode(ajaxreturn(0,lang('请求成功'),$result_data));exit;

        }


    }

    //火车票下单所需参数
    public function train_book_para(){
        $format_data = api_format_data($this->api_key,$this->api_secret,null);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Train/GetTrainBookPara",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));
        }
    }

    //提交火车票订单
    public function train_create_order(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $order_amount = input("order_amount");
        $ticket_fare = input("ticket_fare");
        $start_city = input("start_city");
        $end_city = input("end_city");
        $start_date = input("start_date");
        $train_no = input("train_no");
        $start_time = input("start_time");
        $end_time = input("end_time");
        $contact_name = input("contact_name");
        $contact_phone = input("contact_phone");
        $psg_name = input("psg_name");
        $ticket_type = input("ticket_type");
        $seat_type = input("seat_type");
        $card_type = input("card_type");
        $card_no = input("card_no");
        $phone = input("phone");
        $ins_product_code = input("ins_product_code");
        //乘客实名校验
        if(strlen($card_no)!=15 && strlen($card_no)!=18 ){
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        if(!is_idcard($card_no)){
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        if (!checkName($psg_name)) {
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        $idcard_info=Idcard::get(['idCard'=>$card_no]);
        if($idcard_info){
            if($idcard_info['name']!=$psg_name){
                echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
            }
        }else{
            $idcard_getinfo=check_card($card_no,$psg_name);
            if($idcard_getinfo){
                if($idcard_getinfo['status']=="01"){
                    unset($idcard_getinfo['status']);
                    unset($idcard_getinfo['msg']);
                    unset($idcard_getinfo['prefecture']);

                    $idcard_getinfo['sex']=='男'?$idcard_getinfo['sex']=1:$idcard_getinfo['sex']=2;
                    Idcard::create($idcard_getinfo);
                }else{
                    echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
                }
            }else{
                echo json_encode(ajaxreturn(1,lang('身份证验证失败')));exit;
            }
        }
        $data = array(
            "user_id"=>$userid,
            "sn"=>  "Train".$userid.time(),
            "order_amount"=>$order_amount,
            "ticket_fare"=>$ticket_fare,
            "start_city"=>$start_city,
            "end_city"=>$end_city,
            "start_date"=>$start_date,
            "train_no"=>$train_no,
            "start_time"=>$start_time,
            "end_time"=>$end_time,
            "contact_name"=>$contact_name,
            "contact_phone"=>$contact_phone,
            "psg_name"=>$psg_name,
            "ticket_type"=>$ticket_type,
            "seat_type"=>$seat_type,
            "card_type"=>$card_type,
            "card_no"=>$card_no,
            "phone"=>$phone,
            "ins_product_code"=>$ins_product_code,
            "create_time"=>time(),
            "update_time"=>time(),
            "cancel_time"=>time()+60*$this->application_payment_limit_time,
            "status"=>1
        );
        $res = Db::name("train_application_order")->insert($data);
        if(!$res){
            echo json_encode(ajaxreturn(1,lang('提交失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('提交成功')));
        }
    }

    //火车票订单支付
    public function train_order_pay(){
        $id = input('id');
        $order = Db::name("train_application_order")->where(["id"=>$id,"status"=>1])->find();
        if(empty($order)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));
        }
        $userid = decoded_user_token(input("user_token"),'member_id');
        $member = Db::name('member')->where(['id'=>$userid])->find();
        //判断用户余额
        if($member['app_balance']<$order['order_amount']){
            echo json_encode(ajaxreturn(1,lang('应用余额不足')));exit;
        }
        Db::startTrans();
        try{
            Db::name("member")->where(["id"=>$userid])->setDec("app_balance");
            $log = array(
                "user_id"=>$userid,
                "type"=>7,
                'is_add'=>2,



                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
                'serial_number'=>$order['sn'],
                'number'=> $order['order_amount']
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id"=>$userid,
                "type"=>4,
                'number'=> -$order['order_amount']
            ]);
            Db::name('train_application_order')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));
        }
    }

    //火车票订单列表
    public function train_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("train_application_order")->where(["user_id"=>$userid])->select()?Db::name("train_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$list));
    }

    //火车票订单详情
    public function train_order_detail(){
       $id = input("id");
       if(empty($id)){
           echo json_encode(ajaxreturn(1,lang('参数错误')));
       }
       $detail = Db::name("train_application_order")->where(["id"=>$id])->find();
       if(empty($detail)){
           echo json_encode(ajaxreturn(1,lang('订单不存在')));
       }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$detail));
    }

    //飞机票查询
    public function flight_search(){
        $depart_city_code = input("depart_city_code");
        $arrive_city_code = input("arrive_city_code");
        $is_child = input("is_child");
        $depart_date = input("depart_date");
        $sort_time = input("sort_time");
        $data = array(
            "DepartCityCode"=>$depart_city_code,
            "ArriveCityCode"=>$arrive_city_code,
            "IsChild"=>$is_child,
            "DepartDate"=>$depart_date
        );
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Flight/Query",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(0,lang('请求成功'),[]));exit;
        }else{
            $data = $result["Data"];
            if(!empty($sort_time)){
                for ($i = 0; $i < count($data) ; $i++) {
                    for ($j = $i+1; $j < count($data); $j++) {
                        if (time_to_seconds($data[$i]["DepartTime"]) > time_to_seconds($data[$j]["DepartTime"])) {
                            $tem = $data[$i]; // 这里临时变量，存贮$i的值
                            $data[$i] = $data[$j]; // 第一次更换位置
                            $data[$j] = $tem; // 完成位置互换
                        }
                    }
                }
            }
            echo json_encode(ajaxreturn(0,lang('请求成功'),$data));exit;
        }
    }

    //获取所有机场城市
    public function flight_get_airport_city(){
        $format_data = api_format_data($this->api_key,$this->api_secret,null);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Flight/GetAllAirportCity",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));exit;
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));exit;
        }
    }

    //获取所有航空公司
    public function flight_get_air_company(){
        $format_data = api_format_data($this->api_key,$this->api_secret,null);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Flight/GetAllAirCompany",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));exit;
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));exit;
        }
    }

    //获取机票下单时所需参数
    public function flight_book_para(){
        $start_date = input("start_date");
        $data = array(
            "StartDate" => $start_date
        );
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        $res=mycurl($this->api_url."/Flight/GetFlightBookPara",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));exit;
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));exit;
        }
    }

    //获取下单时的政策信息
    public function flight_policy(){
        $flight_type = input("flight_type");
        $passenger_type = input("passenger_type");

        $air_lines = input("air_lines");

        $air_lines = json_decode(html_change($air_lines),true);
        $data = array(
            "FightType"=>$flight_type,
            "PassengerType"=>$passenger_type,
            "PolicyFlights"=>$air_lines

        );
        
        $format_data = api_format_data($this->api_key,$this->api_secret,$data);
        $heades=[
            'Content-Type:application/json; charset=utf-8',
        ];
        
        $res=mycurl($this->api_url."/Flight/GetFlightPolicy",$format_data,1,$heades);
        $result = json_decode($res,true);

        if(!$result["IsSuccess"]){
            echo json_encode(ajaxreturn(1,lang('请求失败')));
        }else{
            echo json_encode(ajaxreturn(0,lang('请求成功'),$result['Data']));
        }
    }

    //提交飞机票订单
    public function flight_create_order(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $flight_type = input("flight_type");
        $amount = input("amount");
        $baby_fare = input("baby_fare");
        $contact_name = input("contact_name");
        $contact_mobile = input("contact_mobile");
        $order_remark = input("order_remark");
        // $flight_no = input("flight_no");
        // $cabin = input("cabin");
        // $s_airport_code = input("s_airport_code");
        // $e_airport_code = input("e_airport_code");
        // $s_terminal = input("s_terminal");
        // $e_terminal = input("e_terminal");
        // $from_date_time = input("from_date_time");
        // $arrive_date_time = input("arrive_date_time");
        // $plane_type = input("plane_type");
        // $fare = input("fare");
        // $oil = input("oil");
        // $tax = input("tax");
        // $discount = input("discount");
        $air_lines = input("air_lines");
        $psg_type = input("psg_type");
        $psg_name = input("psg_name");
        $card_no = input("card_no");
        $air_company_code = input("air_company_code");
        $mobile = input("mobile");
        $ins_product_code = input("ins_product_code");
        $birth_day = input("birth_day");
        $book_data = input("book_data");
        $cabin_name = input("cabin_name");
        //乘客实名校验
        if(strlen($card_no)!=15 && strlen($card_no)!=18 ){
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        if(!is_idcard($card_no)){
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        if (!checkName($psg_name)) {
            echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
        }
        $idcard_info=Idcard::get(['idCard'=>$card_no]);
        if($idcard_info){
            if($idcard_info['name']!=$psg_name){
                echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
            }
        }else{
            $idcard_getinfo=check_card($card_no,$psg_name);
            if($idcard_getinfo){
                if($idcard_getinfo['status']=="01"){
                    unset($idcard_getinfo['status']);
                    unset($idcard_getinfo['msg']);
                    unset($idcard_getinfo['prefecture']);

                    $idcard_getinfo['sex']=='男'?$idcard_getinfo['sex']=1:$idcard_getinfo['sex']=2;
                    Idcard::create($idcard_getinfo);
                }else{
                    echo json_encode(ajaxreturn(1,lang('经过第三方验证您输入的姓名和身份证不符合，请重新修改名字或身份证号码')));exit;
                }
            }else{
                echo json_encode(ajaxreturn(1,lang('身份证验证失败')));exit;
            }
        }
        $data = array(
            "user_id"=>$userid,
            "sn"=>  "Flight".$userid.time(),
            "amount"=>$amount,
            "flight_type"=>$flight_type,
            "baby_fare"=>$baby_fare,
            "contact_name"=>$contact_name,
            "contact_mobile"=>$contact_mobile,
            "order_remark"=>$order_remark,
            "air_lines"=>$air_lines,
            "psg_type"=>$psg_type,
            "psg_name"=>$psg_name,
            "card_no"=>$card_no,
            "mobile"=>$mobile,
            "air_company_code"=>$air_company_code,
            "ins_product_code"=>$ins_product_code,
            "birth_day"=>$birth_day,
            "book_data"=>$book_data,
            "create_time"=>time(),
            "update_time"=>time(),
            "cancel_time"=>time()+60*$this->application_payment_limit_time,
            "status"=>1,
            "cabin_name"=>$cabin_name
        );
        Db::startTrans();
        try{
            Db::name("flight_application_order")->insert($data);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));exit;
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));exit;
        }

     }

    //飞机票订单支付
    public function flight_order_pay(){
        $id = input('id');
        $order = Db::name("flight_application_order")->where(["id"=>$id,"status"=>1])->find();
        if(empty($order)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));
        }
        $userid = decoded_user_token(input("user_token"),'member_id');
        $member = Db::name('member')->where(['id'=>$userid])->find();
        //判断用户余额
        if($member['app_balance']<$order['amount']){
            echo json_encode(ajaxreturn(1,lang('应用余额不足')));exit;
        }
        Db::startTrans();
        try{ 
            Db::name("member")->where(["id"=>$userid])->setDec("app_balance");
            $log = array(
                "user_id"=>$userid,
                "type"=>7,
                'is_add'=>2,
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
                'serial_number'=>$order['sn'],
                'number'=> $order['amount']
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id"=>$userid,
                "type"=>4,
                'number'=> -$order['amount']
            ]);
            Db::name('flight_application_order')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));
        }
    }


    //飞机订单列表
    public function flight_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("flight_application_order")->where(["user_id"=>$userid])->select()?Db::name("flight_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$list));
    }


    //飞机票订单详情
    public function flight_order_detail(){
        $id = input("id");
        if(empty($id)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        $detail = Db::name("fight_application_order")->where(["id"=>$id])->find();
        if(empty($detail)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));exit;
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$detail));exit;
    }

     //话费充值提交订单
    public function mobile_create_order(){

        
        $userid = decoded_user_token(input("user_token"),'member_id');
        
        

        $mobile = input("mobile");
        $price = input("price");
        
        
        $checktel = telcheck($this->oil_mobile_app_key,$this->oil_mobile_telcheck_url,$mobile,$price);

        //检测手机号与面额是否支持充值
        if(!$checktel){
            echo json_encode(ajaxreturn(1,lang('价格不支持')));exit;
        }
        //商品查询
        $telQueryRes =telquery($this->oil_mobile_app_key,$this->oil_mobile_telquery_url,$mobile,$price); #可以选择的面额5、10、20、30、50、100、300
        if($telQueryRes['error_code'] != '0'){
            echo json_encode(ajaxreturn(1,lang($telQueryRes["error_code"].":".$telQueryRes['reason'])));exit;
        }

        Db::startTrans();
        try{
            $sn = 'Mobile'.$userid.time();
            $data = array(
                "sn" => $sn,
                "user_id" =>$userid,
                "mobile" => $mobile,
                'price' => $price,
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
                "cancel_time"=>time()+60*$this->application_payment_limit_time,
            );
            Db::name("mobile_application_order")->insert($data);

            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));exit;
        }catch (\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败').$e->getMessage()));exit;
        }

    }



    //话费充值--支付
    public function mobile_order_pay(){
        $m_pass = input("m_pass");
        $userid = decoded_user_token(input("user_token"),'member_id');
        if(md5($m_pass.'pin')!= Db::name("member")->where(["id"=>$userid])->find()["pin_password"]){
            echo json_encode(ajaxreturn(1,lang('支付密码错误')));exit;
        }
        $id = input('id');
        $order = Db::name("mobile_application_order")->where(["id"=>$id,"status"=>1])->find();
        if(empty($order)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));exit;
        }
        
        $member = Db::name('member')->where(['id'=>$userid])->find();
        //判断用户余额
        if($member['app_balance']<$order['price']){
            echo json_encode(ajaxreturn(1,lang('应用余额不足')));exit;exit;
        }
        Db::startTrans();
        try{
            Db::name("member")->where(["id"=>$userid])->setDec("app_balance");
            $log = array(
                "user_id"=>$userid,
                "type"=>7,
                'is_add'=>2,
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
                'serial_number'=>$order['sn'],
                'number'=> $order['price']
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id"=>$userid,
                "type"=>4,
                'number'=> $order['price']
            ]);
            Db::name('mobile_application_order')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));exit;
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));exit;
        }
    }

    //话费订单列表
    public function mobile_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("mobile_application_order")->where(["user_id"=>$userid])->select()?Db::name("mobile_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$list));exit;
    }

    //话费订单详情
    public function mobile_order_detail(){
        $id = input("id");
        if(empty($id)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        $detail = Db::name("mobile_application_order")->where(["id"=>$id])->find();
        if(empty($detail)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));exit;
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$detail));exit;
    }

    //油卡充值
    public function oil_order_create(){
        $proid = input("proid");
        $cardnum = input("cardnum");
        $game_userid = input("game_userid");
        $gasCardTel = input("gasCardTel");
        $gasCardName = input("gasCardName");
        $chargeType = input("chargeType");
        $price = input("price");
        $userid = decoded_user_token(input("user_token"),'member_id');


       Db::startTrans();
        try{
            $sn = 'Oil'.$userid.time();
            $data = array(
                "user_id" => $userid,
                'sn' => $sn,
                "price" => $price,
                "proid" => $proid,
                "card_num" => $cardnum,
                "game_userid" => $game_userid,
                "gas_cardtel" => $gasCardTel,
                "gas_cardname" => $gasCardName,
                "charge_type" => $chargeType,
                "status" => 1,
                "create_time" => time(),
                "update_time" => time(),
                "cancel_time"=>time()+60*$this->application_payment_limit_time,
            );
            Db::name("oil_application_order")->insert($data);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));exit;
        }catch (\Exception $e){
            echo json_encode(ajaxreturn(1,lang('提交失败')));exit;
        }
    }

    //油卡充值-支付
    public function oil_order_pay()
    {
        $m_pass = input("m_pass");
        $userid = decoded_user_token(input("user_token"),'member_id');
        $member = Db::name('member')->where(['id'=>$userid])->find();
        if(md5($m_pass.'pin')!= $member["pin_password"]){
            echo json_encode(ajaxreturn(1,lang('支付密码错误')));exit;
        }
        $id = input('id');
        $order = Db::name("oil_application_order")->where(["id"=>$id,"status"=>1])->find();
        if(empty($order)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));exit;
        }
        
        
        //判断用户余额
        if($member['app_balance']<$order['price']){
            echo json_encode(ajaxreturn(1,lang('应用余额不足')));exit;exit;
        }
        Db::startTrans();
        try{
            Db::name("member")->where(["id"=>$userid])->setDec("app_balance");
            $log = array(
                "user_id"=>$userid,
                "type"=>7,
                'is_add'=>2,
                'create_time'=>time(),
                'update_time'=>time(),
                'status'=>1,
                'serial_number'=>$order['sn'],
                'number'=> $order['price']
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id"=>$userid,
                "type"=>4,
                'number'=> $order['price']
            ]);
            Db::name('oil_application_order')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('提交成功')));exit;
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('提交失败'.$e->getMessage())));exit;
        }
    }

    //油卡订单列表
    public function oil_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("oil_application_order")->where(["user_id"=>$userid])->select()?Db::name("oil_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$list));exit;
    }

    //油卡订单详情
    public function oil_order_detail(){
        $id = input("id");
        if(empty($id)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        $detail = Db::name("oil_application_order")->where(["id"=>$id])->find();
        if(empty($detail)){
            echo json_encode(ajaxreturn(1,lang('订单不存在')));exit;
        }
        echo json_encode(ajaxreturn(0,lang('提交成功'),$detail));exit;
    }

    //滴滴-获取taxi_token
    public function didi_get_token(){
        $url = $this->didi_api_url."/api/v1/token/create?access_key=1005444&secret_key=8c348193df4942130fc2c44d9580b830";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
        $res = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($res,true);

        //没有返回结果 无法测试
        if(empty($res) || !isset($res['status'])){
            echo json_encode(ajaxreturn(1,lang("网络错误")));exit;
            
        }
        if($res['status'] != '1'){
            echo json_encode(ajaxreturn(1,lang($res['msg'])));exit;
        }

        echo json_encode(ajaxreturn(0,lang('获取成功'),$res['data']));exit;
    }

    //滴滴-目的地搜索
    public function didi_search_address(){
        $mobileArea = input("mobile_area");
        $mobile = input("mobile");
        $trueName = input("true_name");
        $cityName = input("city_name");
        $input = input("input");
        $taxi_token = input("taxi_token");

        $nonceStr = rand(1000,9999);
        $post_data = array(
            "token" => $taxi_token,
            "mobileArea" => $mobileArea,
            "mobile" => $mobile,
            "trueName" => $trueName,
            "cityName" => $cityName,
            "input" => $input,
            "nonceStr" => $nonceStr
        );
        //dump($post_data);
        //添加签名
        $sign_arr = $post_data;
        unset($sign_arr['token']);
        $sign = md5(taxi_asc_sort($sign_arr).'&key='.$this->didi_secret_key);
        $url = $this->didi_api_url."/api/v1/orderDidi/address?token=".$taxi_token."&mobileArea=".$mobileArea."&mobile=".$mobile."&trueName=".urlencode($trueName)."&cityName=".urlencode($cityName)."&input=".urlencode($input)."&nonceStr=".$nonceStr."&sign=".$sign;
        //dump($url);

        $result=file_get_contents($url);

        $result = json_decode($result,1);

        if(empty($result) || !isset($result['status'])){
            echo json_encode(ajaxreturn(1,lang('网络错误')));exit;
        }
        if($result['status'] != '1') {
            echo json_encode(ajaxreturn(1, lang($result['msg'])));exit;
        }
        echo json_encode(ajaxreturn(0,lang('请求成功'),$result['data']));exit;
    }

    //滴滴-价格查询
    public function didi_get_price(){
        $mobileArea = input("mobile_area");
        $mobile = input("mobile");
        $trueName = input("true_name");
        $cityId = input("city_id");
        $slng = input("slng");
        $slat = input("slat");
        $elng = input("elng");
        $elat = input("elat");
        $taxi_token = input("taxi_token");
        $nonceStr = rand(1000,9999);

        $post_data = array(
            "token" => $taxi_token,
            "mobileArea" => $mobileArea,
            "mobile" => $mobile,
            "trueName" => $trueName,
            "cityId" => $cityId,
            "slng" => $slng,
            "slat" => $slat,
            "elng" => $elng,
            "elat" => $elat,
            "nonceStr" => $nonceStr
        );

        $sign_arr = $post_data;
        unset($sign_arr['token']);
        $sign = md5(taxi_asc_sort($sign_arr).'&key='.$this->didi_secret_key);
        $url = $this->didi_api_url."/api/v1/orderDidi/price?token=".$taxi_token."&mobileArea=".$mobileArea."&mobile=".$mobile."&trueName=".urlencode($trueName)."&cityId=".$cityId."&slng=".$slng."&slat=".$slat
        ."&elng=".$elng."&elat=".$elat."&nonceStr=".$nonceStr."&sign=".$sign;
        $result = file_get_contents($url);

        $result = json_decode($result,1);

        if(empty($result) || !isset($result['status'])){
            echo json_encode(ajaxreturn(1,lang('网络错误')));exit;
        }
        if($result['status'] != '1') {
            echo json_encode(ajaxreturn(1, lang($result['msg'])));exit;
        }
        echo json_encode(ajaxreturn(0, lang('查询成功'),$result["data"]));exit;
    }

    //滴滴-价格查询
    public function didi_cancel_reason(){
        $mobileArea = input("mobile_area");
        $mobile = input("mobile");
        $trueName = input("true_name");
        $taxi_token = input("taxi_token");
        $nonceStr = rand(1000,9999);

        $post_data = array(
            "token" => $taxi_token,
            "mobileArea" => $mobileArea,
            "mobile" => $mobile,
            "trueName" => $trueName,
            "nonceStr" => $nonceStr
        );
        //dump($post_data);
        //添加签名
        $sign_arr = $post_data;
        unset($sign_arr['token']);
        $sign = md5(taxi_asc_sort($sign_arr).'&key='.$this->didi_secret_key);
        $url = $this->didi_api_url."/api/v1/orderDidi/cancelReason?token=".$taxi_token."&mobileArea=".$mobileArea."&mobile=".$mobile."&trueName=".urlencode($trueName)."&nonceStr=".$nonceStr."&sign=".$sign;
        $result = file_get_contents($url);
        $result = json_decode($result,1);

        if(empty($result) || !isset($result['status'])){
            echo json_encode(ajaxreturn(1,lang('网络错误')));exit;
        }
        if($result['status'] != '1') {
            echo json_encode(ajaxreturn(1, lang($result['msg'])));exit;
        }
        echo json_encode(ajaxreturn(0, lang('查询成功'),$result["data"]));exit;
    }

    //滴滴-取消订单
    public function didi_cancel_order(){
        $mobileArea = input("mobileArea");
        $mobile = input("mobile");
        $trueName = input("trueName");
        $orderId = input("orderId");
        $reasonId = input("reasonId");
        $reason = input("reason");
        $taxi_token = input("taxi_token");
        $nonceStr = rand(1000,9999);
        $post_data = array(
            "token" => $taxi_token,
            "mobileArea" => $mobileArea,
            "mobile" => $mobile,
            "trueName" => $trueName,
            "orderId"=>$orderId,
            "reasonId"=>$reasonId,
            "reason"=>$reason,
            "nonceStr" => $nonceStr
        );
        //dump($post_data);
        //添加签名
        $sign_arr = $post_data;
        unset($sign_arr['token']);

        $sign = md5(taxi_asc_sort($sign_arr).'&key='.$this->didi_secret_key);
        $url = $this->didi_api_url."/api/v1/orderDidi/cancel?token=".$taxi_token."&mobileArea=".$mobileArea."&mobile=".$mobile."&trueName=".urlencode($trueName)."&orderId=".$orderId."&reasonId=".$reasonId.
        "&reason=".urlencode($reason)."&nonceStr=".$nonceStr."&sign=".$sign;
        $result = file_get_contents($url);

        $result = json_decode($result,1);

        if(empty($result) || !isset($result['status'])){
            echo json_encode(ajaxreturn(1,lang('网络错误')));exit;
        }
        if($result['status'] != '1') {
            echo json_encode(ajaxreturn(1, lang($result['msg'])));exit;
        }
        echo json_encode(ajaxreturn(0, lang("查询成功"),$result["data"]));exit;
    }

    //滴滴-冻结保证金
    public function didi_ensure(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $user_balance = Db::name("member")->field("app_balance")->where(["id"=>$userid])->find()["app_balance"];
        $taxi_token = input("taxi_token");
        //获取token
        Db::startTrans();
        try{

            Db::name("member")->where(["id"=>$userid])->update(["taxi_token"=>$taxi_token]);
            $count = Db::name("taxi_ensure")->where(["user_id"=>$userid,"status"=>1])->lock(true)->count();
            if($count){
                echo json_encode(ajaxreturn(0,lang('已有保证金')));exit;
            }
            if($user_balance < $this->didi_ensure_amount){
                echo json_encode(ajaxreturn(1,lang('余额不足')));exit;
            }
            Db::name("member")->where(["id" => $userid])->setDec("app_balance",$this->didi_ensure_amount);
            $log = array(
                "user_id" => $userid,
                "type" => 7,
                'is_add' => 2,
                'create_time' => time(),
                'update_time' => time(),
                'status' => 1,
                'serial_number' => null,
                'number' => $this->didi_ensure_amount
            );
            Db::name('app_account_log')->insert($log);
            BonusLog::create([
                "user_id" => $userid,
                "type" => 4,
                'number' => $this->didi_ensure_amount
            ]);
            $data = array(
                'user_id'=>$userid,
                'number'=> $this->didi_ensure_amount,
                'status'=>1,
                'create_time'=>time()
            );
            Db::name("taxi_ensure")->insert($data);
            Db::commit();
            echo json_encode(ajaxreturn(0,lang('冻结成功')));exit;
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1,lang('冻结失败').$e->getMessage()));exit;
        }
    }

    //解冻保证金
    public function didi_un_ensure(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        //判断是否已有保证金
        $taxi_ensure = Db::name("taxi_ensure")->where(["user_id"=>$userid])->lock(true)->find();
        if(empty($taxi_ensure)){
            echo json_encode(ajaxreturn(1,lang('无冻结保证金')));exit;
        }
       Db::startTrans();
       try{
           Db::name("taxi_ensure")->where(["id"=>$taxi_ensure["id"]])->update(["status"=>0,"release_time"=>time()]);
           Db::name("member")->where(["id"=>$userid])->setInc("app_balance",$this->didi_ensure_amount);
           Db::commit();
           echo json_encode(ajaxreturn(0,lang('解冻成功')));exit;
       }catch (\Exception $e){
            Db::rollback();
           echo json_encode(ajaxreturn(1,lang('解冻失败')));exit;
       }
    }

    //车辆类型
    public function didi_car_type(){
        $result = Db::name("taxi_car_type")->field("car_name,car_code")->where(["status"=>1])->order("sort ASC,id DESC")->select();
        echo json_encode(ajaxreturn(0,lang('查询成功'),$result));exit;
    }



    //发起叫车
    public function didi_create_order(){
        $mobileArea = input("mobileArea");
        $mobile = input("mobile");
        $cityId = input("cityId");
        $cityName = input("cityName");
        $startName = input("startName");
        $startAddress = input("startAddress");
        $slng = input("slng");
        $slat = input("slat");
        $endName = input("endName");
        $endAddress = input("endAddress");
        $elng = input("elng");
        $elat = input("elat");
        $price = input("price");
        $priceMd5 = input("priceMd5");
        $carCode = input("carCode");
        $carName = input("carName");
        $trueName = input("trueName");
        $userid = decoded_user_token(input("user_token"),'member_id');
        $member = Db::name("member")->where(["id"=>$userid])->find();
        $taxi_token = input("taxi_token");
        if(empty($startName) || empty($startAddress) || empty($slng) || empty($slat) || empty($cityId) || empty($cityName) || empty($taxi_token)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        if(empty($endName) || empty($endAddress) || empty($elng) || empty($elat) || empty($trueName) || empty($mobileArea)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        if(empty($price) || empty($carCode) || empty($carName)){
            echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
        $url = $this->didi_api_url."/api/v1/orderDidi/create?token=".$taxi_token;
        $nonceStr = rand(1000,9999);
        //dump($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL , $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
        curl_setopt($ch, CURLOPT_POST, 1);
        $post_data = array(
            "token" => $taxi_token,
            "mobileArea" => $mobileArea,
            "mobile" => $mobile,
            "trueName" => $trueName,
            "cityId" => $cityId,
            "cityName" => $cityName,
            "startName" => $startName,
            "startAddress" => $startAddress,
            "slng" => $slng,
            "slat" => $slat,
            "endName" => $endName,
            "endAddress" => $endAddress,
            "elng" => $elng,
            "elat" => $elat,
            "price" => $price,
            "priceMd5" => $priceMd5,
            "carCode" => $carCode,
            "carName" => $carName,
            "nonceStr" => $nonceStr
        );
        //dump($post_data);
        //添加签名
        $sign_arr = $post_data;
        unset($sign_arr['token']);
        $sign = md5(taxi_asc_sort($sign_arr).'&key='.$this->didi_secret_key);
        $post_data['sign'] = $sign;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        curl_close($ch);
        //dump($result);
        $result = json_decode($result,1);

        if(empty($result) || !isset($result['status'])){
            echo json_encode(ajaxreturn(1,lang('网络错误')));exit;
        }
        if($result['status'] != '1') {
            echo json_encode(ajaxreturn(1, lang($result['msg'])));exit;
        }
        //插入打车订单
        $data = array(
            'order_number'=>$result['data'],//订单号
            'user_id'=>$userid,
            'true_name'=>$trueName,
            'mobile_area'=>$mobileArea,
            'mobile'=>$mobile,
            'city_id'=>$cityId,
            'city_name'=>$cityName,
            'start_name'=>$startName,
            'start_address'=>$startAddress,
            'slng'=>$slng,
            'slat'=>$slat,
            'end_name'=>$endName,
            'end_address'=>$endAddress,
            'elng'=>$elng,
            'elat'=>$elat,
            'price'=>$price,
            'price_md5'=>$priceMd5,
            'car_code'=>$carCode,
            'car_name'=>$carName,
            'create_time'=>time(),
            'over_time'=>time()+$this->end_time,
            'status'=>'created',
        );
        Db::startTrans();
        try{
            Db::name("didi_application_order")->insert($data);
            Db::commit();
            echo json_encode(ajaxreturn(0, "提交成功",$result['data']));exit;
        }catch(\Exception $e){
            Db::rollback();
            echo json_encode(ajaxreturn(1, "提交失败".$e->getMessage()));exit;
        }
    }

    //滴滴-查询订单结果
    public function didi_select_result(){
        $order_number = input("order_number");
        $result = Db::name("didi_application_order")->where(["order_number"=>$order_number])->find();
        if($result['over_time'] < time() && $result['status'] == 'created'){
            //订单超时 结束订单
            Db::name("didi_application_order")->where(["order_number"=>$order_number])->update(["status"=>'invalid']);
            $result['status'] = 'invalid';
            echo json_encode(ajaxreturn(0, "订单超时",$result));exit;
        }

        $status_data = array(
            'created'=>'已下单',
            'invalid'=>'已失效',
            'dispatched'=>'进行中',
            'arriving' => '进行中',
            'arrived'=>'司机已到达出发点',
            'serviceStarted'=>'服务开始',
            'serviceFinished'=>'结束行程',
            'paid'=>'已支付',
            'completed'=>'已完成',
            'canceled'=>'已取消',
        );
        $result['status_text'] = $status_data[$result['status']];
        $result['over_time'] = $result['over_time'] - $result['create_time'];
        echo json_encode(ajaxreturn(0, "查询成功",$result));exit;
    }

    //滴滴-订单列表
    public function didi_order_list(){
        $userid = decoded_user_token(input("user_token"),'member_id');
        $list = Db::name("didi_application_order")->where(["user_id"=>$userid])->select()?Db::name("didi_application_order")->where(["user_id"=>$userid])->select():[];
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
            }
        }
        echo json_encode(ajaxreturn(0, "查询成功",$list));exit;
    }

}
